﻿#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class Student
{
	string name;
	int sno;
public:
	Student(string myname,int myn):name(myname),sno(myn)
	{
	}
	virtual void output(ostream& out = cout)
	{
		out << name << "  " << sno;
	}
	virtual void setValue(istream& in)
	{
		in >> name >> sno;
	}
};
class GraduateStudent:public Student
{
	int project;
public:
	GraduateStudent(string myname, int myn, int mypro) :Student(myname, myn)
	{
		project = mypro;
	}
	virtual void output(ostream& out = cout)
	{
		Student::output(out);
		out << "  " << project;
	}
};
//只能作为普通外部函数，不能是成员函数
//定义父类的类型，实参可以是子类的对象
ostream& operator <<(ostream& out, Student& current)
{
	current.output(out);
	return out;
}
istream& operator >>(istream& in, Student& current)
{
	current.setValue(in);
	return in;
}
int main()
{
	Student s1("zhangsan", 10);
	cout << s1 << endl;
	Student s2("lisi", 20);
	Student s3("wangwu", 30);
	ofstream fileout("data.abc");
	fileout << s1 << endl << s2 << endl << s3 << endl;
	ifstream filein("data.abc");
	char temp[50];
	cout << "--------------------" << endl;
	while (!filein.eof())
	{
		filein >> temp;//每一行都是一条学生信息
		cout << temp << endl;
	}
	filein.close();
	fileout.close();
	cout << "--------------------" << endl;
	GraduateStudent g("zhang", 23, 15);
	cout << g;
	return 0;
}
